package root.electronclass.electronclass.school

import android.content.Intent
import android.os.Bundle
import android.view.View
import retrofit2.Call
import retrofit2.Response
import root.electronclass.electronclass.R
import root.electronclass.electronclass.common.base.ViewBindingFragment
import root.electronclass.electronclass.databinding.ActivityMakeCommentsBinding
import root.electronclass.electronclass.mvp.GlobalParameter
import root.electronclass.electronclass.mvp.base.BaseResponse
import root.electronclass.electronclass.mvp.base.CallbackNext
import root.electronclass.electronclass.mvp.entity.IdeaBox
import root.electronclass.electronclass.mvp.entity.IdeaRecordReq
import root.electronclass.electronclass.mvp.rest.RestManager
import root.electronclass.electronclass.other.AttestationActivity

class MakeCommentsFragment : ViewBindingFragment<ActivityMakeCommentsBinding>() {

    private var ideaBox: IdeaBox? = null
    private var check = true

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        ideaBox = (arguments?.getSerializable(EXTRA_DATA) ?: return) as IdeaBox?
        binding.tvTitle.text = ideaBox?.name
        binding.nameTime.text = "${ideaBox?.created}  发布于 ${ideaBox?.createTime}"
        binding.content.text = ideaBox?.remark

        binding.check.setOnClickListener {
            if (check) {
                val intent = Intent(activity, AttestationActivity::class.java)
                startActivityForResult(intent, AttestationActivity.ideaBox_requestCode)
            } else {
                binding.check.setBackgroundResource(R.mipmap.switch_on)
                check = true
            }
        }

        binding.submit.setOnClickListener {
            if (binding.etContent.text.toString().trim() == "") {
                toast("内容不能为空")
                return@setOnClickListener
            }
            getData()
        }

        initToken()
    }

    private fun initToken() {
        RestManager.getInstance().initRest().deviceLogin(GlobalParameter.getDeviceCode())
            .enqueue(object : CallbackNext<BaseResponse<String>>(mDialogViewModel) {
                override fun onSuccessful(
                    call: Call<BaseResponse<String>>?,
                    response: BaseResponse<String>
                ) {
                    if (response.isSuccess) {
                        RestManager.getInstance().token = response.rows
                        RestManager.getInstance().initService()
                    }
                }

                override fun onError(
                    call: Call<BaseResponse<String>>?,
                    response: BaseResponse<String>?
                ) {
                    toast(response?.msg)
                }

                override fun onFail(
                    call: Call<BaseResponse<String>>?,
                    t: Throwable?,
                    response: Response<BaseResponse<String>>?
                ) {
                }

            })
    }


    private fun getData() {
        showWaitingMessage()
        val box = IdeaRecordReq()
        box.boxId = ideaBox?.id
        box.remark = binding.etContent.text.toString().trim()
        box.status = if (check) 1 else 2

        RestManager.getInstance().initRest().editIdeaBox(box)
            .enqueue(object : CallbackNext<BaseResponse<Any>>(mDialogViewModel) {
                override fun onSuccessful(
                    call: Call<BaseResponse<Any>>,
                    response: BaseResponse<Any>
                ) {
                    toast("提交成功")
                    dismissProgress()
                    activity?.finish()
                }

                override fun onError(call: Call<BaseResponse<Any>>?, response: BaseResponse<Any>) {
                    toast(response.msg)
                    dismissProgress()
                }
            })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == AttestationActivity.ideaBox_requestCode) {
            val result: Boolean = data?.getBooleanExtra("SUCCESS", false) as Boolean

            check = if (result) {
                binding.check.setBackgroundResource(R.mipmap.switch_off)
                false
            } else {
                binding.check.setBackgroundResource(R.mipmap.switch_on)
                true
            }
        }
    }


}